package com.wangsd.web.face.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.common.service.UploadFileService;
import com.wangsd.web.face.entity.AppVersion;
import com.wangsd.web.face.mapper.AppVersionMapper;
import com.wangsd.web.face.service.AppVersionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-28
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    @Resource
    AppVersionMapper appVersionMapper;

    @Autowired
    UploadFileService uploadFileService;

    @Override
    public IPage<AppVersion> selectList(Integer pageNum, Integer pageSize, AppVersion params) {
        Page<AppVersion> page = new Page<>(pageNum, pageSize);
        QueryWrapper<AppVersion> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(params.getVersionNo())) {
            queryWrapper.like("version_no", params.getVersionNo());
        }

        if (StringUtils.isNotBlank(params.getFileName())) {
            queryWrapper.like("file_name", params.getFileName());
        }
        IPage<AppVersion> appVersionIPage = appVersionMapper.selectPage(page, queryWrapper);
        return appVersionIPage;
    }

    @Override
    public AppVersion selectByVersionNo(String versionNo) {
        QueryWrapper<AppVersion> query = new QueryWrapper<>();
        query.eq("version_no", versionNo);
        return appVersionMapper.selectOne(query);
    }

    @Override
    public void addOrUpdate(AppVersion appVersion) {
        if (appVersion.getId() != null) {
            /*查询出之前的版本,并删除之前的版本*/
            /*AppVersion oldVersion = appVersionMapper.selectById(appVersion.getId());
            if (oldVersion != null) {
                uploadFileService.deleteFile(oldVersion.getFileName());
            }*/
            appVersionMapper.updateById(appVersion);

        } else {
            appVersionMapper.insert(appVersion);
        }
    }

    @Override
    public AppVersion selectNewest(String versionNo) {
        return appVersionMapper.selectNewest(versionNo);
    }
}
